import {get} from '../../resources/scripts/baseApi.js';
import {getEmergencyOrder, getWorkOrder} from './script/workOrder.js'
import {hourWeather} from './weather.js';
import {formattedDate} from './common.js';
import {weather} from './script/charts/weather.js';
import {marathon_chart} from './script/charts/marathon.js';
import {ambulance} from './script/charts/ambulance.js';

new Vue({
    el: '#base',
    data() {
        return {
            id: null,
            dayWearther: null,
            now: null,
            air: null,
            tqContent: '',
            config_mlsrx: {},
            config_yjrxgd: {},
            dialogVisible: false,
            gdVisible: false,
            mlsNum: '',
            yjrxgdNum: '',
            mls: [],
            rxgd: [],
            hourWeatherList: null,
            activeIndex: 0,
            mlsObj: {},
            yjrxgdObj: {},
            name: '',
            name1: '',
            key: "2348bdf693f440a1b9ee609b6d5fc40b",
            config_ambulance: {}
        }
    },
    methods: {
        ambulance() {
            this.config_ambulance = {
                header: ["序号", "具体保障点位", "负责单位", "车辆号码", "职务", "姓名", "联系方式"],
                data: ambulance,
                index: false,
                align: ['center'],
                columnWidth: [60, 240, 330, 100, 60, 80, 120],
                carousel: 'single',
                rowNum: 10,
                headerHeight: 30,
                waitTime: 3000,
                headerBGC: '#264462',
                oddRowBGC: '#16243c',
                evenRowBGC: '#264462'
            }
            console.log(this.config_ambulance)
        },
        // 点击切换
        handleActiveIndex() {
            this.activeIndex = 0
            if (this.activeIndex == 0) {
                this.init()
            }
        },
        // 获取和风天气城市
        getCity() {
            axios.get(`https://geoapi.qweather.com/v2/city/lookup?location=克拉玛依区&key=${this.key}`).then(res => {

                if (res.data.code == 200) {
                    console.log(res.data.location[0].id)

                    this.id = res.data.location[0].id

                    this.getWeatherDay()
                    this.getWeatherSS()
                }
            })
        },
        // 获取每日天气预报
        getWeatherDay() {
            axios.get(`https://devapi.qweather.com/v7/weather/3d?key=${this.key}&location=${this.id}`).then(res => {
                console.log('获取每日天气预报', res.data.daily)
                this.dayWearther = res.data.daily[0]
            })

            const now = new Date();
            const day = now.getDate();


            get('/EvnData/air', {
                day: day,
                areaName: '克拉玛依市'
            }).then(res => {

                this.air = res.data

                console.log('天', this.air)
            })

        },
        // 获取实时天气
        getWeatherSS() {
            axios.get(`https://devapi.qweather.com/v7/weather/now?&key=${this.key}&location=${this.id}`).then(res => {
                console.log('实时天气预报', res.data)
                this.now = res.data.now
            })
        },
        // 获取天气预警
        getWearthAlarm() {
            get('/Weather/weatherWarnQxt', {
                warntitle: '克拉玛依',
                effective: '2024-08-30',
                expires: '2024-08-30',
                page: 1,
                max: 10
            }).then(res => {

                const result = res.data.rows.slice(0, 5)
                this.tqContent = res.data.rows[0].warncontent
                console.log('这个是天气预警', result)
            })
        },
        // 马拉松工单
        async getMlsList() {
            this.mls = []
            try {
                let params = {
                    pageSize: 1000,
                    pageNum: 1,
                    rqstSource: 10,
                    rqstTitle: '马拉松',
                    rqstContent: '马拉松'
                }

                const data = await getWorkOrder(params)
                this.mls = data
                this.mlsNum = data.length
                this.config_mlsrx = {
                    header: ['序号', '工单号', '诉求人', '类型', '诉求时间', '状态', '详情'],
                    data: [],
                    index: false,
                    align: ['left'],
                    carousel: 'single',
                    columnWidth: [60, 160, 140, 80, 170, 80, 60],
                    rowNum: 5,
                    headerHeight: 30,
                    waitTime: 3000,
                    headerBGC: '#264462',
                    oddRowBGC: '#16243c',
                    evenRowBGC: '#264462'
                }

                let newData = []
                for (let i = 0; i < data.length; i++) {
                    newData.push([
                        i + 1,
                        data[i].caseCode,
                        data[i].reportPersonalInfo.rqstName,
                        data[i].rqstTypeValue,
                        data[i].registerTime,
                        data[i].statusText,
                        '详情',
                        // data[i].caseGuid,
                        // data[i].rqstContent,
                    ])
                }
                this.config_mlsrx.data.push(...newData)
                console.log("=>(index.js:93) data", this.config_mlsrx);
            } catch (e) {
                console.log(e)
            }
        },
        showDetails(item) {
            this.mlsObj = {}
            const data = item.row
            // this.mlsObj = this.mls.filter(obj => data.includes(obj.caseCode));
            this.mlsObj = this.mls.find(obj => data.includes(obj.caseCode));
            console.log("=>(index.js:152) this.mlsObj", this.mlsObj);
            this.name = data[2]
            this.dialogVisible = true
            this.updateDialogVisibility();
        },
        closeDialog() {
            this.dialogVisible = false
            this.updateDialogVisibility();
        },
        updateDialogVisibility() {
            const overlay = document.getElementById('mls-overlay');
            if (this.dialogVisible) {
                overlay.classList.add('visible');
            } else {
                overlay.classList.remove('visible');
            }
        },
        showDetails1(item) {
            this.yjrxgdObj = {}
            const data = item.row
            // this.mlsObj = this.mls.filter(obj => data.includes(obj.caseCode));
            this.yjrxgdObj = this.rxgd.find(obj => data.includes(obj.caseCode));
            console.log("=>(index.js:152) this.mlsObj", this.yjrxgdObj);
            this.name1 = data[2]
            this.gdVisible = true
            this.updateDialogVisibility1();
        },
        closeDialog1() {
            this.gdVisible = false
            this.updateDialogVisibility1();
        },
        updateDialogVisibility1() {
            const oOverlay = document.getElementById('gd-overlay');
            if (this.gdVisible) {
                oOverlay.classList.add('visible');
            } else {
                oOverlay.classList.remove('visible');
            }
        },
        async getEmergencyLIst() {
            this.rxgd = []
            try {
                let params = {
                    pageSize: 1000,
                    pageNum: 1,
                    rqstSource: 10,
                    rqstTitle: '紧急',
                    rqstContent: '紧急'
                }
                const data = await getEmergencyOrder(params)
                this.rxgd = data
                this.yjrxgdNum = data.length
                this.config_yjrxgd = {
                    header: ['序号', '工单号', '诉求人', '类型', '诉求时间', '状态', '详情'],
                    data: [],
                    index: false,
                    align: ['left'],
                    carousel: 'single',
                    rowNum: 5,
                    columnWidth: [60, 160, 140, 80, 170, 80, 60],
                    headerHeight: 30,
                    waitTime: 3000,
                    headerBGC: '#264462',
                    oddRowBGC: '#16243c',
                    evenRowBGC: '#264462'
                }

                let newData = []
                for (let i = 0; i < data.length; i++) {
                    newData.push([
                        i + 1,
                        data[i].caseCode,
                        data[i].reportPersonalInfo.rqstName,
                        data[i].rqstTypeValue,
                        data[i].registerTime,
                        data[i].statusText,
                        '详情',
                        // data[i].caseGuid,
                        // data[i].rqstContent,
                    ])
                }
                this.config_yjrxgd.data.push(...newData)
            } catch (e) {
                console.log(e)
            }
        },
        async getHouerWeather() {
            try {
                const result = await hourWeather('新疆', '克拉玛依')
                const hourWeatherList = result.data.result.hourly
                //  降水
                const precipitationList = hourWeatherList.precipitation
                //  温度
                const temperatureList = hourWeatherList.temperature
                // 风速
                const windList = hourWeatherList.wind
                let xName = []
                let yData = []
                let yData2 = []
                let yData3 = []

                let yPoint = []
                precipitationList.map(item => {
                    xName.push(
                        formattedDate(item.datetime)
                    )
                    yData.push(item.value)

                })
                temperatureList.map(item => {
                    yData2.push(item.value)
                })
                windList.map(item => {
                    yData3.push(item.speed)
                })
                yPoint = [yData, yData2, yData3]


                weather(xName, yPoint)
                console.log(precipitationList, temperatureList, windList)

            } catch (error) {
                console.log('获取数据失败', error)
            }
        },
        init() {
            marathon_chart()
        }
    },
    mounted() {
        this.getCity()
        this.getWeatherSS()
        setInterval(() => {
            this.getWeatherSS()
            this.getWearthAlarm()
        }, 3600000)
        this.getWearthAlarm()
        this.getHouerWeather()
        this.getMlsList()
        this.getEmergencyLIst()
        setInterval(() => {
            this.getMlsList()
            this.getEmergencyLIst()
        }, 600000)
        this.init()
        this.ambulance()
    }
})